package com.wbs.domain.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.wbs.domain.Transaction;
import com.wbs.domain.repository.base.BaseRepository;

/**
 * Interface related with the methods for TransactionRepository values
 */
public interface TransactionRepository extends BaseRepository<Transaction, Long>, JpaSpecificationExecutor<Transaction>{

    String COUNT_TRANSACTIONS    = "SELECT count(c) FROM Transaction c";
    String FIND_ALL_TRANSACTIONS= "SELECT c FROM Transaction c"
                             + " JOIN fetch c.transactionLevel";

    @Query(value=FIND_ALL_TRANSACTIONS, countQuery=COUNT_TRANSACTIONS)
    Page<Transaction> findAllTransactionsByPage(Pageable pageable);
    
    

    String FIND_TRANSACTION     = "SELECT g FROM Transaction g" +
                                  " WHERE (g.code = :code AND g.sign = :sign)" + 
                                  "    OR (g.externalCode = :externalCode AND g.sign = :sign)" +
                                  "   AND g.id <> :id";

    @Query(FIND_TRANSACTION)
    List<Transaction> findTransaction(@Param("code") String code,
                                  @Param("externalCode") String externalCode,
                                  @Param("sign") String sign,
                                  @Param("id") Long id);    
    
}